﻿using System.Linq;
using ECommerce.Web.Models;
using Machine.Specifications;

namespace Basket_specs
{
    [Subject("Shopping basket")]
    public class When_adding_a_product_to_an_empty_basket
    {
        private static Basket basket;
        private static Product product;

        Establish context = () =>
            {
                basket = new Basket();

                product = new Product() { Id = 1, Name = "Product 1", Price = 10m };
            };

        Because of = () =>
            {
                basket.AddProduct(product, 1);
            };

        It should_add_a_basket_item_for_the_product = () =>
            {
                BasketItem item = basket.Items.ElementAt(0);

                item.Product.ShouldEqual(product);
            };

        It should_add_basket_item_with_the_quantity_specificed = () =>
            {
                BasketItem item = basket.Items.ElementAt(0);

                item.Quantity.ShouldEqual(1);
            };
    }

    [Subject("Shopping basket")]
    public class When_adding_the_same_product_several_times_to_an_empty_basket
    {
        private static Basket basket;
        private static Product product;

        Establish context = () =>
            {
                basket = new Basket();

                product = new Product() { Id = 1, Name = "Product 1", Price = 10m };
            };

        Because of = () =>
            {
                basket.AddProduct(product, 1);
                basket.AddProduct(product, 1);
            };

        It should_add_a_single_basket_item = () =>
            {
                basket.Items.Count().ShouldEqual(1);
            };

        It should_add_the_quantites_for_the_product_together = () =>
            {
                BasketItem item = basket.Items.ElementAt(0);

                item.Quantity.ShouldEqual(2);
            };
    }

    [Subject("Shopping basket")]
    public class When_getting_the_sub_total_for_a_basket
    {
        private static Basket basket;
        private static Product product1;
        private static Product product2;
        private static decimal basketSubTotal;

        Establish context = () =>
            {
                basket = new Basket();

                product1 = new Product() { Id = 1, Name = "Product 1", Price = 10m };
                product2 = new Product() { Id = 2, Name = "Product 2", Price = 20m };

                basket.AddProduct(product1, 2);
                basket.AddProduct(product2, 4);
            };

        Because of = () =>
            {
                basketSubTotal = basket.GetSubTotal();
            };

        It should_return_the_sum_of_product_quantity_multiplied_by_price = () =>
            {
                basketSubTotal.ShouldEqual(100m);
            };
    }
}
